package com.pxmeta.party.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.pxmeta.party.entity.Gift;
import com.pxmeta.party.entity.UserGift;
import com.pxmeta.party.mapper.UserGiftMapper;
import com.pxmeta.party.service.IUserGiftService;
import lombok.RequiredArgsConstructor;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class UserGiftServiceImpl extends ServiceImpl<UserGiftMapper, UserGift> implements IUserGiftService {

    @Override
    public void addUserGift(Long userId, List<Gift> giftList) {
        if (giftList.isEmpty()){
            return;
        }

        for (Gift gift : giftList) {
            LambdaQueryWrapper<UserGift> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(UserGift::getUserId, userId)
                    .eq(UserGift::getGiftId, gift.getId());

            UserGift userGift = this.getOne(wrapper);
            if (userGift != null){
                userGift.setCount(userGift.getCount()+gift.getCount());
                this.updateById(userGift);
            }else {
                UserGift newUserGift = new UserGift();
                newUserGift.setGiftId(gift.getId());
                newUserGift.setUserId(userId);
                newUserGift.setCount(gift.getCount());
                this.save(newUserGift);
            }
        }

//        if (!saveList.isEmpty()){
//            this.saveBatch(saveList);
//        }

    }
}
